package gold.digger;

import gold.utils.InputUtil;

import java.util.*;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LC1260 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public List<List<Integer>> shiftGrid(int[][] grid, int k) {
            int m = grid.length, n = grid[0].length;
            int[][] changedGrid = new int[m][n];

            for (int i = 0; i < n; i++) {
                // 遍历第 i 列
                int shiftedCol = (i + k) % n, shiftedRow = ((i + k) / n) % m;
                for (int j = 0; j < m; j++) {
                    changedGrid[(j + shiftedRow) % m][shiftedCol] = grid[j][i];
                }
            }

            List<List<Integer>> ans = new ArrayList<>();
            for (int i = 0; i < m; i++) {
                List<Integer> line = new ArrayList<>();
                for (int j = 0; j < n; j++) line.add(changedGrid[i][j]);
                ans.add(line);
            }

            return ans;
        }
    }

    public void run() {
        // [[9,1,2],[3,4,5],[6,7,8]]
        System.out.println(new Solution().shiftGrid(InputUtil.toDoubleIntegerArray("[[1,2,3],[4,5,6],[7,8,9]]"), 1));
        System.out.println(new Solution().shiftGrid(InputUtil.toDoubleIntegerArray("[[3,8,1,9],[19,7,2,5],[4,6,11,10],[12,0,21,13]]"), 4));
    }

    public static void main(String[] args) throws Exception {
        LC1260 an = new LC1260();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
